<template>
	<view class="beijingse mainpadding">
		<!-- 更改名称 -->
		<u-popup :show="show" @close="close" mode="center" :round="15">
			<view class="mainpadding">
				<view class="xiaohei bigtext fonweight textcenter">更改用户名</view>
				<input class="input margin_top" type="text" placeholder="请输入用户名" v-model="nickname">
				<view class="flexcenter margin_top6">
					<view class="quxibtn flexcenter margin_right3" @click="close">取消</view>
					<view class="quedbtn flexcenter" @click="submit">确定</view>
				</view>
			</view>
		</u-popup>
		<view class="mainpadding ffffff radius flexbetween" @click="uploadImg(1,avatar,avatar_text)">
			<view class="ershiba xiaohei fonweight">头像</view>
			<view class="flexright">
				<image :src="avatar_text" class="margin_right2" style="border-radius: 50%;width: 80rpx;height: 80rpx;" mode=""></image>
				<u-icon name="arrow-right" color="#C8C8C8"></u-icon>
			</view>
		</view>
		<view class="mainpadding ffffff radius flexbetween margin_top" @click="show = true">
			<view class="ershiba xiaohei fonweight">用户名</view>
			<view class="flexright">
				<view class="ershiba xiaohei margin_right2">{{nickname_text}}</view>
				<u-icon name="arrow-right" color="#C8C8C8"></u-icon>
			</view>
		</view>
		<view class="mainpadding ffffff radius flexbetween margin_top" @click="jumper('/pages/mypage/xiugaimm')">
			<view class="ershiba xiaohei fonweight">修改密码</view>
			<u-icon name="arrow-right" color="#C8C8C8"></u-icon>
		</view>
		<view class="mainpadding ffffff radius flexbetween margin_top" @click="jumper('/pages/mypage/genghuansjh')">
			<view class="ershiba xiaohei fonweight">手机号码</view>
			<view class="flexright">
				<view class="ershiba xiaohei margin_right2">{{userInfo.mobile}}</view>
				<u-icon name="arrow-right" color="#C8C8C8"></u-icon>
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				show: false,
				avatar: "",
				avatar_text: "",
				nickname: "",
				nickname_text: "",
				userInfo: {},
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			init() {
				httpRequest.request('/api/user/index', 'GET', {}).then(res => {
					this.avatar = res.data.avatar
					this.avatar_text = res.data.avatar_text
					this.nickname = res.data.nickname
					this.nickname_text = res.data.nickname
					this.userInfo = res.data
				})
			},
			uploadImg(value, upImgs, upImgs_text) {
				let _this = this;
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(1, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(1, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					this.avatar = res.url;
					this.avatar_text = image_item
					this.submit()
				}).catch(err => {});
			},
			submit() {
				this.close()
				httpRequest.request('/api/user/profile', 'GET', {
					avatar: this.avatar,
					nickname: this.nickname
				}).then(res => {
					this.init()
				})
			},
			close() {
				this.show = false
			},
			jumper(url){
				uni.navigateTo({
					url:url
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.input {
		padding: 18rpx 30rpx;
		background: #F9F9F9;
		border-radius: 18rpx 18rpx 18rpx 18rpx;
	}
</style>
